//
// Created by lingzhitech on 25-10-27.
//

#include "Folder.hpp"

#include <iostream>

#include "Message.hpp"
Folder::~Folder() {
    removeFolderFromMessages();
}

Folder::Folder(std::initializer_list<Message *> messages) {
    for (auto message : messages) {
        message->save(*this);
    }
}


Folder &Folder::operator=(const Folder &folder) {
    if (this == &folder)
        return *this;
    removeFolderFromMessages();
    messages = folder.messages;
    addFolderToMessages();
    return *this;
}


Folder::Folder(const Folder &folder): messages(folder.messages) {
    addFolderToMessages();
}


void Folder::addFolderToMessages() {
    for (auto message: messages) {
        message->addFolder(this);
    }
}

void Folder::removeFolderFromMessages() {
    for (auto message: messages) {
        message->removeFolder(this);
    }
    messages.clear();
}

std::ostream& operator<<(std::ostream& os, const Folder& folder) {
    os << "Folder@" << &folder << " contains " << folder.messages.size() << " messages:" << std::endl;
    for (auto message: folder.messages) {
        os << *message << std::endl;
    }
    return os;
}

/**
 *
 * @param message 将message指针添加到当前folder的messages
 */
void Folder::addMessage(Message* message) {
    messages.insert(message);
}

void Folder::removeMessage(Message* message) {
    messages.erase(message);
}